package edu.office.sessionRepository;

import edu.office.applicationRepository.SchoolRepository;
import edu.office.applicationRepository.SubjectRepository;
import edu.office.applicationRepository.TeacschoolsubjectRepository;
import edu.office.entities.School;
import edu.office.entities.Subject;
import edu.office.entities.TeacherAdmin;
import edu.office.entities.Teacschoolsubject;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.*;

@Named("teacschoolsubjectController")
@SessionScoped
public class TeacschoolsubjectSSRep extends CommonModelSSRep<Teacschoolsubject> implements Serializable {
     @Inject
    SchoolRepository applicationSchool;
    @Inject
    SubjectRepository applicationSubject;
    @Inject
     TeacschoolsubjectRepository applicationTeacschoolsubjectController;
    private HashMap<School, HashSet<Subject>> getCurrentSchool4TeacherMap(TeacherAdmin teacherAdmin) {
        Set<Teacschoolsubject> temTeacschoolsubjects
                = applicationTeacschoolsubjectController.getTeacschoolsubjects4Teacher(teacherAdmin);
        HashMap<School, HashSet<Subject>> schoolSubjectMap4LoginedTeac = new HashMap<>();
        for (Teacschoolsubject tss : temTeacschoolsubjects) {
            School school1=applicationSchool.find("ID",tss.getSCHOOLID()).firstResult();
            schoolSubjectMap4LoginedTeac.computeIfAbsent(school1, k -> new HashSet<>());
            schoolSubjectMap4LoginedTeac.get(school1).add(applicationSubject.find("ID",tss.getSUBJECTID()).firstResult());
        }
        return schoolSubjectMap4LoginedTeac;
    }

    public List<School> getCurrentSchool4Teacher(TeacherAdmin teacherAdmin) {
        return new LinkedList<>(getCurrentSchool4TeacherMap(teacherAdmin).keySet());
    }

    public Set<Subject> getCurrentSubject4Teacher(TeacherAdmin teacherAdmin, School school) {
        return getCurrentSchool4TeacherMap(teacherAdmin).get(school);

    }

    public List<Teacschoolsubject> search(TeacherAdmin teacherAdmin, School school, Subject subject, LocalDate fromTimeCalendar) {
        String conditionString = "";
        if (teacherAdmin != null) {
            conditionString += " teacherid=" + teacherAdmin.getID();
        }
        if (school != null) {
            if (!conditionString.trim().isEmpty()) {
                conditionString += " and schoolid=" + school.getID();
            } else {
                conditionString += " schoolid=" + school.getID();
            }
        }
        if (subject.getID() != null) {
            if (!conditionString.trim().isEmpty()) {
                conditionString += " and subjectid=" + subject.getID();
            } else {
                conditionString += " subjectid=" + subject.getID();
            }
        }
        if (null != fromTimeCalendar) {
            String dateString = fromTimeCalendar.getYear()+ "-" + (fromTimeCalendar.getMonth()) + "-" + fromTimeCalendar.getDayOfMonth();
            if (!conditionString.trim().isEmpty()) {
                conditionString += " and fromTime >'" + dateString + "'";
            } else {
                conditionString += " fromTime >'" + dateString + "'";
            }
        }
        if (!conditionString.trim().isEmpty()) {
            return applicationTeacschoolsubjectController.list("select * from teacschoolsubject where " + conditionString + " order by totime desc");
        } else {
            return applicationTeacschoolsubjectController.list("select * from teacschoolsubject");
        }
    }
}
